package com.sram.controller.user;

import javax.annotation.Resource;
import javax.jws.soap.SOAPBinding;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;
import com.sram.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sram.entity.User;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

	@Resource
	private UserService userService;

	@RequestMapping(value="/login")
	public  @ResponseBody String regist( HttpServletRequest request,Model model, HttpServletResponse response,HttpSession session, User user) throws IOException {
		System.out.println(user);
		String mes =null;
		List<User> dd =userService.getUserByName(user.getUserName(),user.getPassword());
		if(dd.size() == 0){
			mes = "密码错误";
		}else{
			session.setAttribute("userId",dd.get(0).getId());
			session.setAttribute("userName",dd.get(0).getUserName());
			mes = "ok";
		}
		return mes;
	}

	@RequestMapping(value="/out")
	public String out(HttpServletRequest request,HttpSession session){
		session.invalidate();
		return "redirect:/index.jsp";
	}
	/*----------------------------------------------------------*/
	@RequestMapping(value="/getAllUser")
	public String getAllUser(HttpServletRequest request,Model model) throws Exception {
		List<User> ss = userService.getAll();
		model.addAttribute("users",ss);
		return "allUser";
	}
	@RequestMapping(value="/toAddAndUpUser")
	public String toAddAndUpUser(HttpServletRequest request,Model model) throws Exception {
		String id = request.getParameter("id");
		if( id != null){
			User  user = (User) userService.selectByPrimaryKey(Integer.valueOf(id));
			model.addAttribute("user",user);
		}
		return "user/addAndUpUser";
	}
	@RequestMapping(value="/addAndUpUser")
	public String addAndUpUser(HttpServletRequest request,Model model,User user) throws Exception {
		if(user.getId() == null){
			userService.insert(user);
		}else{
			userService.updateByPrimaryKey(user);
		}

		return "redirect:getAllUser.do";
	}
	@RequestMapping(value="/delUser")
	public @ResponseBody String delUser(HttpServletRequest request,Model model) throws Exception {
		String id = request.getParameter("id");
		userService.deleteByPrimaryKey(Integer.valueOf(id));
		return "success";
	}
	/*----------------------------------------------------------*/
	@RequestMapping(value="/main")
	public String main(){
		return "main";
	}
	@RequestMapping(value="/chuanzhi")
	public String chuanzhi(){
		return "chuanzhi";
	}
	@RequestMapping(value="/getUser1")
	public String getUser1( Model model,User user){
		model.addAttribute("name",user.getUserName());
		model.addAttribute("pwd","dddddddddddd");
		return "chuanzhi";
	}
	@RequestMapping(value="/getUser2")
	public String getUser2(HttpServletRequest request, Model model,User user){
		String username = request.getParameter("userName");
		String password = request.getParameter("password");
		model.addAttribute("name",username);
		model.addAttribute("pwd",password);
		return "chuanzhi";
	}
	@RequestMapping(value="/getUser3")
	public void getUser3(HttpServletRequest request, Model model,HttpServletResponse response,User user) throws IOException {
		String username = request.getParameter("userName");
		System.out.printf("getUser3-----------"+username);
		model.addAttribute("name",username);
		response.getWriter().write("阿斯蒂芬");
		response.getWriter().flush();
	}
	@RequestMapping(value="/getUser4")
	public void getUser4(HttpServletRequest request, Model model,HttpServletResponse response,User user) throws IOException {
		String username = request.getParameter("userName");
		model.addAttribute("name",username);
		List ss = new ArrayList();
		ss.add(new User("ss","dd"));
		ss.add(new User("ss2","dd2"));
		System.out.printf("getUser4-----------"+ss.toString());
		response.getWriter().write(JSON.toJSONString(ss));
		response.getWriter().flush();
	}
	@RequestMapping(value="/getUser5")
	public  @ResponseBody String  getUser5(HttpServletRequest request, Model model,HttpServletResponse response,User user) throws IOException {
		String username = request.getParameter("userName");
//		return "ss";//中文乱码  ,produces = "application/json; charset=utf-8"  加上请求会反应慢
		return "杜浩";
	}
	@RequestMapping(value="/getUser6")
	public  @ResponseBody List<User> getUser6(HttpServletRequest request, Model model,HttpServletResponse response,User user) throws IOException {
		String username = request.getParameter("userName");
		model.addAttribute("name",username);
		List ss = new ArrayList();
		ss.add(new User("胜多负少","dd"));
		ss.add(new User("水电费","dd2"));
		return ss;
	}
	@RequestMapping(value="/redirect")
	public  String redirect(RedirectAttributes attr) throws IOException {
		// 重定向中文乱码问题server.xml 设置URIEncoding="UTF-8"
		attr.addAttribute("ss","杜浩");
		return "redirect:redirect2.do";
	}
	@RequestMapping(value="/redirect2")
	public  String userList2(HttpServletRequest request, Model model,HttpServletResponse response,User user) throws IOException {
		System.out.println("redirect2................"+ request.getParameter("ss"));
		return "redirect";
	}
}
